package com.ymt.bpm.web.rest.platform.fauth;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PResource;
import com.ymt.bpm.service.platform.fauth.FaResService;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/3/7.
 */
@RestController
public class FaResResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FaResService rs;

    /**
     * List all resources in resource management
     * @param req
     * @param timezoneOffset
     * @return
     */
    @RequestMapping("/api/v1/fa/reses/tree")
    public JSONArray tree(HttpServletRequest req,
                          @RequestParam("p")int purpose,
                          @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("list tenantId=" + tenantId);
            log.debug("list loginName=" + loginName);
            log.debug("list purpose=" + purpose);
            log.debug("list timezoneOffset=" + timezoneOffset);
        }
        return rs.tree(tenantId, loginName, lang, purpose, timezoneOffset);
    }

    /**
     * reorder resources
     * @param req
     * @param orders
     * @return
     */
    @RequestMapping("/api/v1/fa/reses/reorder")
    public JSONObject reorder(HttpServletRequest req,
                              @RequestParam("orders")String orders) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("reorder tenantId=" + tenantId);
            log.debug("reorder orders=" + orders);
        }
        int ret = rs.reorder(tenantId, loginName, displayName, orders);
        JSONObject rtn = new JSONObject();
        rtn.put("result", ret);
        return rtn;
    }

    /**
     * save a resource
     * @param req
     * @param resource
     * @param timezoneOffset
     * @return
     */
    @RequestMapping("/api/v1/fa/res/save")
    public JSONObject save(HttpServletRequest req, PResource resource,
                           @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("save tenantId=" + tenantId);
            log.debug("save lang=" + lang);
            log.debug("save timezoneOffset=" + timezoneOffset);
        }
        JSONObject rtn = rs.save(tenantId, loginName, displayName, lang, timezoneOffset, resource);
        return rtn;
    }

    /**
     * get a resource
     * @param req
     * @param resId
     * @return
     */
    @RequestMapping(value="/api/v1/fa/res/{resId}", method=RequestMethod.DELETE)
    public JSONObject delete(HttpServletRequest req, @PathVariable("resId")String resId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delete tenantId=" + tenantId);
            log.debug("delete resId=" + resId);
        }
        JSONObject rtn = rs.delete(tenantId, loginName, displayName, resId);
        return rtn;
    }

    /**
     * check if a resName exists
     * @param req
     * @return
     */
    @RequestMapping(value="/api/v1/fa/res/checkName",
            method= RequestMethod.POST,
            produces = "plain/text")
    public String checkName(HttpServletRequest req,
                            @RequestParam(name="p")int purpose,
                            @RequestParam(name="resId", required=false)String resId,
                            @RequestParam(name="parentId", required=false)String parentId,
                            @RequestParam("resName")String resName) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
//        String resId = req.getParameter("resId");
//        String resName = req.getParameter("resName");
        if (log.isDebugEnabled()) {
            log.debug("checkName tenantId=" + tenantId);
            log.debug("checkName resId=" + resId);
            log.debug("checkName parentId=" + parentId);
            log.debug("checkName purpose=" + purpose);
            log.debug("checkName resName=" + resName);
        }
        return rs.checkName(tenantId, resName, resId, parentId, purpose);
    }

    /**
     * 获取角色管理所需的资源树
     * @param req
     * @param timezoneOffset
     * @return
     */
    @RequestMapping("/api/v1/fa/reses/treeForRole")
    public JSONArray treeForRoleManage(HttpServletRequest req,
                                       @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("list tenantId=" + tenantId);
            log.debug("list loginName=" + loginName);
            log.debug("list timezoneOffset=" + timezoneOffset);
        }
        return rs.tree(tenantId, loginName, lang, -1, timezoneOffset);
    }

}
